clear;
clc;

M = generate_mseq([1, 0, 0, 0, 0, 1, 1]);
for i = 1:63
    Din(i) = (-1)^M(i);
end
Din(64) = 1;
for i = 1:63
    G(i) = (-1)^i;
end
G(64) = 1;
spec_D = fft(Din);
spec_G = fft(G);
figure(1);
plot(1:64, abs(spec_D(1:64)).^2,1:64, abs(spec_G(1:64)).^2);
title('Spectrum');
legend('M sequence', '-1 1 -1 1 ...');

spec = spec_D + spec_G;
spec(33) = 0;
Dout = ifft(spec);

diff1 = sum(abs(Dout - Din)); 
Din = (-1).*Din;
diff2 = sum(abs(Dout - Din));
disp(diff1);
disp(diff2);

%% -1 -1 -1 1 -1 -1 -1 1
G1 = ones(1,63).*(-1);
for i = 4:4:63
    G1(i) = 1;
end
G1(64) = 1;
spec_G1 = fft(G1);
figure(2);
plot(1:64, abs(spec_D(1:64)).^2,1:64, abs(spec_G1(1:64)).^2);
title('Spectrum');
legend('M sequence', '-1 -1 -1 1 -1 -1 -1 1 ...');

spec = spec_D + spec_G1;
spec(find(abs(spec(1:64)).^2 > max(abs(spec_D(1:64)).^2))) = 0;
Dout = ifft(spec);

diff1 = sum(abs(Dout - Din)); 
Din = (-1).*Din;
diff2 = sum(abs(Dout - Din));
disp(diff1);
disp(diff2);

%% 1 1 -1 -1 1 1 -1 -1
G2 = ones(1,63).*(-1);
for i = 1:4:63
    G2(i) = 1;
    G2(i+1) = 1;
end
G2(64) = 1;
spec_G2 = fft(G2);
figure(3);
plot(1:64, abs(spec_D(1:64)).^2,1:64, abs(spec_G2(1:64)).^2);
title('Spectrum');
legend('M sequence', '1 1 -1 -1 1 1 -1 -1 ...');

spec = spec_D + spec_G2;
spec(find(abs(spec(1:64)).^2 > max(abs(spec_D(1:64)).^2))) = 0;
Dout = ifft(spec);

diff1 = sum(abs(Dout - Din)); 
Din = (-1).*Din;
diff2 = sum(abs(Dout - Din));
disp(diff1);
disp(diff2);